<html>
<head><meta charset="utf-8"><title>ThreadId::new UB · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ThreadId.3A.3Anew.20UB.html">ThreadId::new UB</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="241138108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ThreadId%3A%3Anew%20UB/near/241138108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ThreadId.3A.3Anew.20UB.html#241138108">(Jun 02 2021 at 11:57)</a>:</h4>
<p>I think I found UB in <code>ThreadId::new</code>: If the thread ids become exhausted and you try to get a new thread (by spawning a new thread) id from a panic handler you set previously the lock is acquired re-entrantly, which isn't allowed as the lock is never initialized.</p>
<p><a href="https://github.com/rust-lang/rust/blob/7350f655ef7e1b00f287eca6f8c0016a8985900c/library/std/src/thread/mod.rs#L1011">https://github.com/rust-lang/rust/blob/7350f655ef7e1b00f287eca6f8c0016a8985900c/library/std/src/thread/mod.rs#L1011</a></p>



<a name="241139852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ThreadId%3A%3Anew%20UB/near/241139852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank Steffahn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ThreadId.3A.3Anew.20UB.html#241139852">(Jun 02 2021 at 12:16)</a>:</h4>
<p>For the record, exhausting a u64 is virtually impossible. Take a clock rate faster than any existing CPU, e.g. 10GHz, and increase the counter by one every cycle. Then it still takes <a href="https://www.google.com/search?q=2^64%20/%20(10*10^9*3600*24*365)">almost 60 years</a> to reach u64::MAX. (And nobody can create a new thread every clock cycle.)</p>
<p>Anyway, we can probably safely replace the <code>panic</code> with an <code>abort</code> to fix the issue, right?</p>



<a name="241139911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ThreadId%3A%3Anew%20UB/near/241139911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ThreadId.3A.3Anew.20UB.html#241139911">(Jun 02 2021 at 12:16)</a>:</h4>
<p>Yes, or maybe nicer explicitly unlock the lock before panicking.</p>



<a name="241140642"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ThreadId%3A%3Anew%20UB/near/241140642" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank Steffahn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ThreadId.3A.3Anew.20UB.html#241140642">(Jun 02 2021 at 12:24)</a>:</h4>
<p>If we’re assuming a few 100s of clock cycles for spawning new threads (or whatever operation that creates new ThreadIds) then the 60 years become ten-thousands of years. I think <code>abort</code> is a very reasonable approach. There won’t be anyone exhausting a u64 ThreadID <em>by accident</em> in the next few centuries.</p>



<a name="241142435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ThreadId%3A%3Anew%20UB/near/241142435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ThreadId.3A.3Anew.20UB.html#241142435">(Jun 02 2021 at 12:41)</a>:</h4>
<p>It might matter in extreme use cases where programmers have to prove there not existing a single code path leading to an <code>abort</code>. Kind of like <a href="https://github.com/Technolution/rustig">https://github.com/Technolution/rustig</a> but then for <code>abort</code>s <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span> Some programs might rely on these kind of assertions for certification. (<code>panic</code>s are also frowned upon in those use cases, but can potentially be recovered from at least, e.g. using <a href="https://doc.rust-lang.org/std/panic/fn.catch_unwind.html"><code>catch_unwind</code></a>).</p>



<a name="245009120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ThreadId%3A%3Anew%20UB/near/245009120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ThreadId.3A.3Anew.20UB.html#245009120">(Jul 06 2021 at 08:30)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/122651-general/topic/ThreadId.3A.3Anew.20UB/near/241139911">said</a>:</p>
<blockquote>
<p>Yes, or maybe nicer explicitly unlock the lock before panicking.</p>
</blockquote>
<p>or at least add a comment :D</p>



<a name="245025170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ThreadId%3A%3Anew%20UB/near/245025170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ThreadId.3A.3Anew.20UB.html#245025170">(Jul 06 2021 at 11:29)</a>:</h4>
<p>So... I think with some aggressive optimizations, it may be possible to do a leakocalypse style thread id generation without actually creating any threads. Basically it goes as follows: use the thread builder to create a thread with a stack size that will cause the inner thread creation to panic. catch the panic, repeat.</p>



<a name="245027548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ThreadId%3A%3Anew%20UB/near/245027548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ThreadId.3A.3Anew.20UB.html#245027548">(Jul 06 2021 at 11:55)</a>:</h4>
<p>Exhausting u64 is still pretty hard. But I agree that elegant code without a failure mode such as this would be nicer.</p>



<a name="245482911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ThreadId%3A%3Anew%20UB/near/245482911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ThreadId.3A.3Anew.20UB.html#245482911">(Jul 09 2021 at 18:53)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/87011">https://github.com/rust-lang/rust/pull/87011</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>